package com.hp.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hp.dao.EmpDao;
import com.hp.pojo.Emp;
import com.hp.service.EmpService;
import com.hp.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpDao empDao;

    @Override
    public JsonData allEmp(int page, int limit) {
        PageHelper.startPage(page, limit);
        List<Emp> books=empDao.allEmp(page,limit);
        PageInfo<Emp> pageInfo=new PageInfo<>(books);
        return JsonData.buildSuccess(pageInfo);
    }

    @Override
    public JsonData delEmp(int empId) {
        int i=empDao.delEmp(empId);
        if(i>0){
            return JsonData.buildSuccess("成功");
        }
        return JsonData.buildError("失败");
    }

    @Override
    public JsonData saveEmp(Emp emp) {
        int i=0;
        if(emp.getEmpId()==null){
            i=empDao.saveEmp(emp);
        }else {
            i=empDao.enidEmp(emp);
        }
        if(i>0){
            return JsonData.buildSuccess("成功");
        }
        return JsonData.buildError("失败");
    }
}
